package me.zhengjie.modules.finace_taxation.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.finace_taxation.domain.FtContactUs;
import me.zhengjie.modules.finace_taxation.service.dto.FtContactUsDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtContactUsQueryParam;
import me.zhengjie.modules.finace_taxation.service.FtContactUsService;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "contactUs管理")
@RequestMapping("/api/ft/ftContactUs")
public class FtContactUsController {

    private final FtContactUsService ftContactUsService;

    @GetMapping
    @Log("查询联系我们")
    @ApiOperation("查询联系我们")
//    @PreAuthorize("@el.check('ftContactUs:list')")
    public ResponseEntity query(FtContactUsQueryParam query, Pageable pageable){
        return new ResponseEntity<>(ftContactUsService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增联系我们")
    @ApiOperation("新增联系我们")
//    @PreAuthorize("@el.check('ftContactUs:add')")
    public ResponseEntity create(@Validated @RequestBody FtContactUsDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftContactUsService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改联系我们")
    @ApiOperation("修改联系我们")
//    @PreAuthorize("@el.check('ftContactUs:edit')")
    public ResponseEntity update(@Validated @RequestBody FtContactUsDto resources){
        ftContactUsService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除联系我们")
    @ApiOperation("删除联系我们")
//    @PreAuthorize("@el.check('ftContactUs:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftContactUsService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/getContactUsByCity")
    @Log("根据城市查询该城市下公司的联系方式")
    @ApiOperation("根据城市查询该城市下公司的联系方式")
//    @PreAuthorize("@el.check('ftContactUs:getContactUsByCity')")
    public ResponseEntity getContactUsByCity(@RequestBody FtContactUsQueryParam query) {
        List<FtContactUs> ftContactUses = ftContactUsService.getContactUsByCity(query);
        return new ResponseEntity<>(ftContactUses,HttpStatus.OK);
    }

    @Log("导出联系我们")
    @ApiOperation("导出联系我们")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, FtContactUsQueryParam query) throws IOException {
        ftContactUsService.download(ftContactUsService.queryAll(query), response);
    }

}
